import("//llvm/lib/Target/targets_string.gni")
import("//llvm/triples.gni")
import("//llvm/utils/gn/build/write_cmake_config.gni")
import("bolt_lit_site_cfg_files.gni")

template("write_lit_config") {
  write_cmake_config(target_name) {
    input = invoker.input
    output = invoker.output
    values = [
      "BOLT_BINARY_DIR=" +
          rebase_path(get_label_info("//bolt", "target_out_dir")),
      "BOLT_SOURCE_DIR=" + rebase_path("//bolt"),
      "LIT_SITE_CFG_IN_HEADER=## Autogenerated from $input, do not edit",
      "LLVM_BINARY_DIR=" + get_label_info("//llvm", "target_out_dir"),
      "LLVM_LIBS_DIR=",  # needed only for shared builds
      "LLVM_LIT_TOOLS_DIR=",  # Intentionally empty, matches cmake build.
      "LLVM_SOURCE_DIR=" + rebase_path("//llvm"),
      "LLVM_TOOLS_DIR=" + rebase_path("$root_out_dir/bin"),
      "LLVM_TARGET_TRIPLE=$llvm_target_triple",
    ]
    values += invoker.extra_values
  }
}

write_lit_config("lit_site_cfg") {
  # Fully-qualified instead of relative for LIT_SITE_CFG_IN_HEADER.
  input = "//bolt/test/lit.site.cfg.py.in"
  output = bolt_lit_site_cfg_file
  dir = get_path_info(output, "dir")
  extra_values = [
    "BOLT_CLANG_EXE=" + rebase_path("$root_build_dir/bin/clang"),
    "BOLT_LLD_EXE=" + rebase_path("$root_build_dir/bin/ld.lld"),
    "BOLT_ENABLE_RUNTIME=0",  # FIXME: enable runtime
    "GNU_LD_EXECUTABLE=",  # FIXME: set sometimes?
    "LLVM_HOST_TRIPLE=$llvm_current_triple",
    "LLVM_USE_SANITIZER=",
    "Python3_EXECUTABLE=$python_path",
    "TARGETS_TO_BUILD=$llvm_targets_to_build_string",
  ]

  if (host_os == "win") {
    # See comment for Windows solink in llvm/utils/gn/build/toolchain/BUILD.gn
    extra_values += [ "SHLIBDIR=" + rebase_path("$root_out_dir/bin", dir) ]
  } else {
    extra_values += [ "SHLIBDIR=" + rebase_path("$root_out_dir/lib", dir) ]
  }

  if (host_cpu == "x64") {
    extra_values += [ "HOST_ARCH=x86_64" ]
  } else if (host_cpu == "arm64") {
    extra_values += [ "HOST_ARCH=arm64" ]
  } else if (host_cpu == "ppc64") {
    extra_values += [ "HOST_ARCH=powerpc64le" ]
  } else {
    assert(false, "unimplemented host_cpu " + host_cpu)
  }
}

write_lit_config("lit_unit_site_cfg") {
  # Fully-qualified instead of relative for LIT_SITE_CFG_IN_HEADER.
  input = "//bolt/test/Unit/lit.site.cfg.py.in"
  output = bolt_lit_unit_site_cfg_file
  extra_values = [ "LLVM_BUILD_MODE=." ]
}

# This target should contain all dependencies of check-bolt.
# //:default depends on it, so that ninja's default target builds all
# prerequisites for check-bolt but doesn't run check-bolt itself.
group("test") {
  deps = [
    ":lit_site_cfg",
    ":lit_unit_site_cfg",
    "//bolt/tools/bat-dump:llvm-bat-dump",
    "//bolt/tools/driver:symlinks",
    "//bolt/tools/heatmap:llvm-bolt-heatmap",
    "//bolt/tools/merge-fdata",
    "//bolt/unittests",
    "//clang/tools/driver:symlinks",
    "//lld/tools/lld:symlinks",
    "//llvm/tools/llc",
    "//llvm/tools/llvm-config",
    "//llvm/tools/llvm-dwarfdump",
    "//llvm/tools/llvm-dwp",
    "//llvm/tools/llvm-mc",
    "//llvm/tools/llvm-nm:symlinks",
    "//llvm/tools/llvm-objcopy:symlinks",
    "//llvm/tools/llvm-objdump:symlinks",
    "//llvm/tools/yaml2obj",
    "//llvm/utils/FileCheck",
    "//llvm/utils/count",
    "//llvm/utils/not",
  ]

  testonly = true
}

action("check-bolt") {
  script = "$root_out_dir/bin/llvm-lit"
  if (host_os == "win") {
    script += ".py"
  }
  args = [
    "-sv",
    rebase_path(".", root_out_dir),
  ]
  outputs = [ "$target_gen_dir/run-lit" ]  # Non-existing, so that ninja runs it
                                           # each time.

  # Since check-bolt is always dirty, //:default doesn't depend on it so that
  # it's not part of the default ninja target.  Hence, check-bolt shouldn't
  # have any deps except :test. so that the default target is sure to build
  # all the deps.
  deps = [ ":test" ]
  testonly = true

  pool = "//:console"
}
